<?
  include "../common/db_conn.php";

  $sd_idx = $_GET["sd_idx"];
  if ($sd_idx == "")
    $sd_idx = 0;
  
  $query_d = "select sd_idx, sd_text from sv_dlist where sd_idx = $sd_idx";
  $result_d = mysql_query($query_d);
  $rows_d = mysql_fetch_array($result_d);
  
  $query_q = "select sq_idx, sq_no, sq_text, sq_type, sq_answer from sv_qlist where sd_idx = $sd_idx order by sq_no desc";
  $result_q = mysql_query($query_q);
  $num_rows_q = mysql_num_rows($result_q);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script>
<? if ($num_rows_q > 1) { ?>
  function moveQuestion() 
  {
    if (document.frm_m.change_from.value < 1 || document.frm_m.change_from.value > <?=$num_rows_q?>) {
      document.frm_m.change_from.value = "";
      document.frm_m.change_from.focus();
      alert("Out of range");      
    } else  if (document.frm_m.change_to.value < 1 || document.frm_m.change_to.value > <?=$num_rows_q?>) {
      document.frm_m.change_to.value = "";
      document.frm_m.change_to.focus();
      alert("Out of range");      
    } else {
      document.frm_m.submit();
    }
    
  }
<? } ?>

  function deleteQuestion(idx)
  {
    var ans = confirm("Will you delete?");
    
    if (ans == true) {
      document.getElementById("ifr").src = "q_proc.php?ptype=4&sd_idx=<?=$sd_idx?>&sq_idx="+idx;
    }
  }
</script>
</head>
<body>

<h1>관리자 페이지</h1>
<h2><?=$rows_d["sd_idx"]?>. <?=stripslashes($rows_d["sd_text"])?></h2>
<button onClick="location.href='./'">목록으로</button><br>
<? if ($num_rows_q > 1) { ?>
<form name="frm_m" method="post" action="q_proc.php" target="ifr">

<input type="hidden" name="ptype" value="3">
<input type="hidden" name="sd_idx" value="<?=$sd_idx?>">
번호 변경: <input type="number" name="change_from" size="2" maxlength="2" min="1" max="<?=$num_rows_q?>">번을 <input type="number" name="change_to" size="2" maxlength="2" min="1" max="<?=$num_rows_q?>">번 위치로 <input type="button" value="이동하기" onClick="moveQuestion();"><br>
</form>
<? } ?>
<table>
  <tr>
    <td>번호</td>
    <td>질문</td>
  </tr>
<? 
  for ($i = 0; $i < $num_rows_q; $i++)
  {
    $rows_q = mysql_fetch_array($result_q); 
?>
  <tr> 
    <td><?=$rows_q["sq_no"]?></td>
    <td><?=stripslashes($rows_q["sq_text"])?></td>
    <td>
      <button onClick="location.href='q_edit.html?sd_idx=<?=$sd_idx?>&sq_idx=<?=$rows_q["sq_idx"]?>'">수정</button>
      <button onClick="deleteQuestion(<?=$rows_q["sq_idx"]?>);">삭제</button>
    </td>
  </tr> 
<?
  } 
?>
</table>
<button onClick="location.href='q_edit.html?sd_idx=<?=$sd_idx?>'">추가</button>
<button>저장</button>

<br>
<iframe src="about:blank" id="ifr" name="ifr" width="300" height="300"></iframe>

</body>
</html>
<?
  include "../common/db_close.php";
?>
